// nettest.cc 
//	Test out message delivery between two "Nachos" machines,
//	using the Post Office to coordinate delivery.
//
//	Two caveats:
//	  1. Two copies of Nachos must be running, with machine ID's 0 and 1:
//		./nachos -m 0 -o 1 &
//		./nachos -m 1 -o 0 &
//
//	  2. You need an implementation of condition variables,
//	     which is *not* provided as part of the baseline threads 
//	     implementation.  The Post Office won't work without
//	     a correct implementation of condition variables.
//
// Copyright (c) 1992-1993 The Regents of the University of California.
// All rights reserved.  See copyright.h for copyright notice and limitation 
// of liability and disclaimer of warranty provisions.

#include "copyright.h"

#include "system.h"
#include "network.h"
#include "post.h"
#include "interrupt.h"
#include "list.h"
#include "synch.h"
#include <iostream>
#include <sstream>

// Test out message delivery, by doing the following:
//	1. send a message to the machine with ID "farAddr", at mail box #0
//	2. wait for the other machine's message to arrive (in our mailbox #0)
//	3. send an acknowledgment for the other machine's message
//	4. wait for an acknowledgement from the other machine to our 
//	    original message

bool mine(char*);
void SecondServer(int);
	
struct ServerLock {
	char* name;
	bool available,destroyed;
	int owner;
	int mailbox;
	List* replyQ;
};

struct ServerCV{
	char* name;
	List* replyQ;
	int lockNum;
	bool destroyed,readyToBeDestroyed;
};

struct ServerMV{
	char* name;
	int* values;
};	
	
struct ServerQuery{
	int machineid;
	int mailbox;
	int id;
	char* msg;
	bool* responses;
	bool resolved;
	bool createQuery;
};
	
const int LOCKCREATE = 3;
const int LOCKACQUIRE = 0;
const int LOCKRELEASE = 1;
const int LOCKDESTROY = 4;
const int CONDITIONWAIT = 2;
const int CONDITIONCREATE = 5;
const int CONDITIONSIGNAL = 6;
const int CONDITIONBROADCAST = 7;
const int CONDITIONDESTROY= 8;
const int MONITORCREATE = 9;
const int MONITORGET = 10;
const int MONITORSET = 11;
const int MONITORDESTROY = 12;
const int SERVERMSG = 13;

const int SERVERQUERY = 0;
const int MSGFORWARD = 1;
const int SERVERRESPONSE = 1;

const int NOTMINE = 0;
const int MINE = 1;
const int DIDYOUCREATELOCK = 2;
const int DIDYOUCREATECV = 3;
const int DIDYOUCREATEMV = 4;
const int NOTMYLOCK = 5;
const int NOTMYCV = 6;
const int NOTMYMV = 7;

const int LOCK = 0;
const int CV = 1;
const int MV = 2;


const int MAX_LOCKS = 1000;
int nextServerLock = 0;
ServerLock** serverLockArray = new ServerLock*[MAX_LOCKS];

const int MAX_CVS = 1000;
int nextServerCV = 0;
ServerCV** serverCVArray = new ServerCV*[MAX_CVS];	

const int MAX_MVS = 1000;
int nextServerMV = 0;
ServerMV** serverMVArray = new ServerMV*[MAX_MVS];

Lock* serverLockArrayLock = new Lock("ArrayLock");
Lock* serverCVArrayLock = new Lock("CvLock");
Lock* serverMVArrayLock = new Lock("MvLock");	
Lock* serverQueryArrayLock = new Lock("QueryLock");

int nextServerQuery = 0;
const int MAX_QUERIES = 1000;
ServerQuery** serverQueries = new ServerQuery*[MAX_QUERIES];

int servernumber;

void Server(int numservers){	
	struct Reply{
		char msg[MaxMailSize];
		int pktHdrTo,mailHdrTo,mailHdrFrom,length;
	};

	PacketHeader outPktHdr, inPktHdr;
    MailHeader outMailHdr, inMailHdr;
	char input[MaxMailSize];
	bool activitySuccess;
	
	cout<<"What is my server number?";
	cin>>servernumber;
	
	Thread* t = new Thread("SecondServer");
	t->Fork(SecondServer,numservers);

	
	while(true){
		bool serverMsg = false;
		int machineTo,mailboxTo;
		char* arg[7];
		bool sendMessage = true;
		char* replyNum;
		int missing;
		bool found = false;
		bool create = false;
		
	
		postOffice->Receive(0, &inPktHdr, &inMailHdr,input);			
		
		char saveInput[MaxMailSize];
		strcpy(saveInput,input);
		
		char* buffer  = strtok(input," ");
		
		int i = 0;
		while(buffer != NULL){
			arg[i] = buffer;
			i++;
			buffer = strtok(NULL," ");
		}		
		
		if(atoi(arg[0]) == SERVERMSG){
			serverMsg = true;
			machineTo = atoi(arg[1]);
			mailboxTo = atoi(arg[2]);
			
			for(int a = 0;a < 4;a++){
				arg[a] = arg[a+3];
			}
		}
				
		if(atoi(arg[0]) == LOCKCREATE){
			create = true;
			missing = LOCK;
			
			for(int a = 0;a < nextServerLock;a++){
				if(strcmp(serverLockArray[a]->name,arg[1]) == 0){
					printf("Names matched. Found Lock\n");
					replyNum = serverLockArray[a]->name;
					activitySuccess = true;
					found = true;
					break;
				}
			}
		}
		else if(atoi(arg[0]) == LOCKACQUIRE){
			int lockNum;
			create = false;
			missing = LOCK;
			for(int a = 0;a < nextServerLock;a++){
				if(strcmp(serverLockArray[a]->name,arg[1]) == 0){
					printf("Names matched\n");
					found = true;
					lockNum = a;
					break;
				}
			}
			
			if(found){				
				if(serverLockArray[lockNum]->available){
					printf("Acquiring lock %d\n",lockNum);
					serverLockArray[lockNum]->available = false;
					serverLockArray[lockNum]->owner = inPktHdr.from;
					
					//replyNum = 1;
					replyNum = "1";
					activitySuccess = true;
				}
				else{
					Reply* temp = new Reply();
					
					sendMessage = false;
					
					char data[MaxMailSize];				
					sprintf(data,"%d",1);
					temp->pktHdrTo = inPktHdr.from;		
					temp->mailHdrTo = 0;
					temp->mailHdrFrom = 0;
					temp->length = strlen(data) + 1;
					strcpy(temp->msg,data);
					
					serverLockArray[lockNum]->replyQ->Append(temp);
					
					printf("Added program to queue\n");
				}				
			}
		}
		else if(atoi(arg[0]) == LOCKRELEASE){
			int lockNum;
			create = false;
			missing = LOCK;
			printf("Trying to release lock %d\n",lockNum);
			
			for(int a = 0;a < nextServerLock;a++){
				if(strcmp(serverLockArray[a]->name,arg[1]) == 0){
					printf("Names matched\n");
					replyNum = "1";
					found = true;
					lockNum = a;
					break;
				}
			}
			
			if(found){
				if(inPktHdr.from == serverLockArray[lockNum]->owner){
					serverLockArray[lockNum]->available = true;
					serverLockArray[lockNum]->owner = NULL;
					
					//replyNum = 1;
						replyNum = "1";
					activitySuccess = true;
					if(!serverLockArray[lockNum]->replyQ->IsEmpty()){
						printf("Waking a program\n");
						
						List* tempList = serverLockArray[lockNum]->replyQ;
						Reply* tempReply =(Reply*) tempList->Remove();
						
						serverLockArray[lockNum]->available = false;
						serverLockArray[lockNum]->owner = tempReply->pktHdrTo;
						
						PacketHeader tempPktHead;
						tempPktHead.to = tempReply->pktHdrTo;					
						
						MailHeader tempMailHead;
						tempMailHead.to = tempReply->mailHdrTo;
						tempMailHead.from = tempReply->mailHdrFrom;
						tempMailHead.length = tempReply->length;
						
						printf("lock release\n");
						bool success = postOffice->Send(tempPktHead,tempMailHead,tempReply->msg);
				
						if ( !success ) {
							printf("The postOffice Send failed. You must not have the other Nachos running. Terminating Nachos.\n");
							interrupt->Halt();
						}
					}
				}
				else{
					printf("Lock is not owned by machine %d\n",inPktHdr.from);
				}
			}
		}
		else if(atoi(arg[0]) == LOCKDESTROY){
			int lockNum;
			create = false;
			missing = LOCK;
			
			for(int a = 0;a < nextServerLock;a++){
				if(strcmp(serverLockArray[a]->name,arg[1]) == 0){
					printf("Names matched\n");
					replyNum = "1";
					found = true;
					lockNum = a;
					break;
				}
			}
			
			if(found){
				if(lockNum < 0 || lockNum >= nextServerLock){
					printf("Invalid lock number\n");
					
					activitySuccess = false;
				}
				else {
					serverLockArray[lockNum]->destroyed = true;
					printf("Lock destroyed\n");
				}
			}
		}
		else if(atoi(arg[0]) == CONDITIONCREATE){
			create = true;
			missing = CV;
			
			for(int a = 0;a < nextServerCV;a++){
				if(strcmp(serverCVArray[a]->name,arg[1]) == 0){
					printf("Names matched. Found CV\n");
					//replyNum = a;
					replyNum = serverCVArray[a]->name;
					activitySuccess = true;
					found = true;
					break;
				}
			}
		
			/*printf("Did not find CV. Creating new one\n");
				if(nextServerCV < MAX_CVS){
					printf("Creating condition %d\n",nextServerCV);

					serverCVArray[nextServerCV] = new ServerCV();
					serverCVArray[nextServerCV]->replyQ = new List();
					serverCVArray[nextServerCV]->lockNum = -1;
					serverCVArray[nextServerCV]->destroyed = false;
					serverCVArray[nextServerCV]->readyToBeDestroyed = false;
					
					int length = strlen(arg[1]);
						
					char* tempchar = new char[length];
					strcpy(tempchar,arg[1]);
						
					serverCVArray[nextServerCV]->name = tempchar;
					
					//replyNum = nextServerCV;
					replyNum = tempchar;
					activitySuccess = true;
					
					nextServerCV++;
				}
				else{
					printf("No condition variables left to create\n");
				
					activitySuccess = false;
				}
			}*/
		}
		else if(atoi(arg[0]) == CONDITIONWAIT){
			
			int lockNum;
			int cvNum;
			missing = CV;
			create = false;
			bool found1 = false;
			bool found2 = false;
			
			for(int a = 0;a < nextServerCV;a++){
				if(strcmp(serverCVArray[a]->name,arg[1]) == 0){
					printf("Names matched. Found CV\n");
					//replyNum = a;
					cvNum = a;
					replyNum = serverCVArray[a]->name;
					activitySuccess = true;
					found1 = true;
					break;
				}
			}
			
			for(int a = 0;a < nextServerLock;a++){
				if(strcmp(serverLockArray[a]->name,arg[2]) == 0){
					printf("Names matched\n");
					found2 = true;
					lockNum = a;
					break;
				}
			}
			
			if(found1&&found2){
				found = true;
			
				bool waited = false;
				
				if(found){
					if(cvNum < 0 || cvNum >= nextServerCV){
						printf("Invalid condition variable number\n");
						
						activitySuccess = false;
					}
					else if(lockNum < 0 || lockNum >= nextServerLock){
						printf("Invalid lock number\n");
						
						activitySuccess = false;
					}
					else if(serverLockArray[lockNum]->destroyed){
						printf("Unable to wait. Lock has been deleted\n");
						
						activitySuccess = false;
					}
					else if(serverCVArray[cvNum]->destroyed){
						printf("Condition variable has been deleted\n");
						
						activitySuccess = false;
					}
					else if(serverCVArray[cvNum]->lockNum == -1){
						sendMessage = false;
					
						serverCVArray[cvNum]->lockNum = lockNum;
						
						Reply* temp = new Reply();
					
						char data[MaxMailSize];				
						sprintf(data,"%d",1);
						temp->pktHdrTo = inPktHdr.from;		
						temp->mailHdrTo = 0;
						temp->mailHdrFrom = 0;
						temp->length = strlen(data) + 1;
						strcpy(temp->msg,data);
						
						serverCVArray[cvNum]->replyQ->Append(temp);
			
						waited = true;
						printf("Putting machine %d to sleep\n",inPktHdr.from);
					}
					else if(serverCVArray[cvNum]->lockNum == lockNum){
						//printf("The owner of the lock is %d\n",serverLockArray[lockNum]->owner);
						//printf("The message came from 
						
						if(serverLockArray[lockNum]->owner == inPktHdr.from){
							Reply* temp = new Reply();
						
							sendMessage = false;
							
							char data[MaxMailSize];				
							sprintf(data,"%d",1);
							temp->pktHdrTo = inPktHdr.from;		
							temp->mailHdrTo = 0;
							temp->mailHdrFrom = 0;
							temp->length = strlen(data) + 1;
							strcpy(temp->msg,data);
							
							serverCVArray[cvNum]->replyQ->Append(temp);
							
							waited = true;
							printf("Putting machine %d to sleep\n",inPktHdr.from);
						}
						else{
							printf("The program is not the owner of the right lock\n");
							
							activitySuccess = false;
						}
					}
					else{
						activitySuccess = false;
						
						printf("Wrong lock for condition %d\n",cvNum);
					}	

					if(waited){
						serverLockArray[lockNum]->available = true;
						serverLockArray[lockNum]->owner = NULL;
						printf("Releasing lock\n");
						
						if(!serverLockArray[lockNum]->replyQ->IsEmpty()){
							printf("Waking a program\n");
							
							List* tempList = serverLockArray[lockNum]->replyQ;
							Reply* tempReply =(Reply*) tempList->Remove();
							
							serverLockArray[lockNum]->available = false;
							serverLockArray[lockNum]->owner = tempReply->pktHdrTo;
							
							PacketHeader tempPktHead;
							tempPktHead.to = tempReply->pktHdrTo;					
							
							MailHeader tempMailHead;
							tempMailHead.to = tempReply->mailHdrTo;
							tempMailHead.from = tempReply->mailHdrFrom;
							tempMailHead.length = tempReply->length;
							
							printf("lock release\n");
							bool success = postOffice->Send(tempPktHead,tempMailHead,tempReply->msg);
					
							if ( !success ) {
								printf("The postOffice Send failed. You must not have the other Nachos running. Terminating Nachos.\n");
								interrupt->Halt();
							}
						}
					}
				}
			}
			else{
				activitySuccess = false;
				found = true;
			}
		}
		else if(atoi(arg[0]) == CONDITIONSIGNAL){
			int lockNum;
			int cvNum;
			missing = CV;
			create = false;
			bool found1 = false;
			bool found2 = false;
			
			for(int a = 0;a < nextServerCV;a++){
				if(strcmp(serverCVArray[a]->name,arg[1]) == 0){
					printf("Names matched. Found CV\n");
					//replyNum = a;
					cvNum = a;
					replyNum = serverCVArray[a]->name;
					activitySuccess = true;
					found1 = true;
					break;
				}
			}
			
			for(int a = 0;a < nextServerLock;a++){
				if(strcmp(serverLockArray[a]->name,arg[2]) == 0){
					printf("Names matched\n");
					found2 = true;
					lockNum = a;
					break;
				}
			}
			
			if(found1&&found2){
				found = true;
			
				if(found){
					if(cvNum < 0 || cvNum >= nextServerCV){
						printf("Invalid condition variable number\n");
						
						activitySuccess = false;
					}
					else if(lockNum < 0 || lockNum >= nextServerLock){
						printf("Invalid lock number\n");
						
						activitySuccess = false;
					}
					else if(serverLockArray[lockNum]->destroyed){
						printf("Unable to signal. Lock has been deleted\n");
						
						activitySuccess = false;
					}
					else if(serverCVArray[cvNum]->destroyed){
						printf("Unable to signal. Condition variable has been deleted\n");

						activitySuccess = false;
					}
					else if(serverCVArray[cvNum]->lockNum != lockNum && serverCVArray[cvNum]->lockNum != -1){
						activitySuccess = false;
						
						printf("Wrong lock for condition %d\n",cvNum);
					}
					else if(serverLockArray[lockNum]->owner != inPktHdr.from && serverCVArray[cvNum]->lockNum != -1){
						activitySuccess = false;
						
						printf("Machine does not own lock to signal condition %d\n",cvNum);
					
					}
					else if(!serverCVArray[cvNum]->replyQ->IsEmpty()){
						printf("Waking a program\n");
					
						List* tempList = serverCVArray[cvNum]->replyQ;
						Reply* tempReply =(Reply*) tempList->Remove();
						
						if(serverLockArray[lockNum]->available){
							serverLockArray[lockNum]->available = false;
							serverLockArray[lockNum]->owner = tempReply->pktHdrTo;
						
							PacketHeader tempPktHead;
							tempPktHead.to = tempReply->pktHdrTo;					
							
							MailHeader tempMailHead;
							tempMailHead.to = tempReply->mailHdrTo;
							tempMailHead.from = tempReply->mailHdrFrom;
							tempMailHead.length = tempReply->length;
							
							printf("signal send\n");
							bool success = postOffice->Send(tempPktHead,tempMailHead,tempReply->msg);
					
							if ( !success ) {
								printf("The postOffice Send failed. You must not have the other Nachos running. Terminating Nachos.\n");
								interrupt->Halt();
							}
						}
						else{
								printf("Putting program from condition Q to lock Q\n");
							serverLockArray[lockNum]->replyQ->Append(tempReply);

							//replyNum = 1;
							replyNum = "1";
							activitySuccess = true;
						}			

						if(serverCVArray[cvNum]->replyQ->IsEmpty()){
							if(serverCVArray[cvNum]->readyToBeDestroyed)
								serverCVArray[cvNum]->destroyed = true;
								
							serverCVArray[cvNum]->lockNum = -1;				
						}
					}
					else{
						activitySuccess = false;
						printf("No programs waiting to be awaken\n");
					}
				}
			}
			else{
				activitySuccess = false;
				found = true;
			}
		}
		else if(atoi(arg[0]) == CONDITIONBROADCAST){
			int lockNum;
			int cvNum;
			missing = CV;
			create = false;
			bool found1 = false;
			bool found2 = false;
			
			for(int a = 0;a < nextServerCV;a++){
				if(strcmp(serverCVArray[a]->name,arg[1]) == 0){
					printf("Names matched. Found CV\n");
					//replyNum = a;
					cvNum = a;
					replyNum = serverCVArray[a]->name;
					activitySuccess = true;
					found1 = true;
					break;
				}
			}
			
			for(int a = 0;a < nextServerLock;a++){
				if(strcmp(serverLockArray[a]->name,arg[2]) == 0){
					printf("Names matched\n");
					found2 = true;
					lockNum = a;
					break;
				}
			}
			
			if(found1&&found2){
				found = true;
		
				if(found){
					if(cvNum < 0 || cvNum >= nextServerCV){
						printf("Invalid condition variable number\n");
						
						activitySuccess = false;
					}
					else if(lockNum < 0 || lockNum >= nextServerLock){
						printf("Invalid lock number\n");
						
						activitySuccess = false;
					}
					else if(serverLockArray[lockNum]->destroyed){
						printf("Unable to broadcast. Lock has been deleted\n");
						
						activitySuccess = false;
					}
					else if(serverCVArray[cvNum]->destroyed){
						printf("Unable to broadcast. Condition variable has been deleted\n");

						activitySuccess = false;
					}
					else if(serverCVArray[cvNum]->lockNum != lockNum && serverCVArray[cvNum]->lockNum != -1){
						activitySuccess = false;
						
						printf("Wrong lock for condition %d\n",cvNum);
					}
					else if(serverLockArray[lockNum]->owner != inPktHdr.from && serverCVArray[cvNum]->lockNum != -1){
						activitySuccess = false;
						
						printf("Machine does not own lock to signal condition %d\n",cvNum);
					
					}
					else {
						while(!serverCVArray[cvNum]->replyQ->IsEmpty()){
							printf("Waking a program\n");
						
							List* tempList = serverCVArray[cvNum]->replyQ;
							Reply* tempReply =(Reply*) tempList->Remove();
							
							if(serverLockArray[lockNum]->available){
								serverLockArray[lockNum]->available = false;
								serverLockArray[lockNum]->owner = tempReply->pktHdrTo;
							
								PacketHeader tempPktHead;
								tempPktHead.to = tempReply->pktHdrTo;					
								
								MailHeader tempMailHead;
								tempMailHead.to = tempReply->mailHdrTo;
								tempMailHead.from = tempReply->mailHdrFrom;
								tempMailHead.length = tempReply->length;
								
								printf("signal send\n");
								bool success = postOffice->Send(tempPktHead,tempMailHead,tempReply->msg);
						
								if ( !success ) {
									printf("The postOffice Send failed. You must not have the other Nachos running. Terminating Nachos.\n");
									interrupt->Halt();
								}
								
								//replyNum = 1;
							replyNum = "1";
								activitySuccess = true;
							}
							else{
								printf("Putting program from condition Q to lock Q\n");
								serverLockArray[lockNum]->replyQ->Append(tempReply);
								//replyNum = 1;
							replyNum = "1";
								activitySuccess = true;
							}

							if(serverCVArray[cvNum]->replyQ->IsEmpty()){
								if(serverCVArray[cvNum]->readyToBeDestroyed)
									serverCVArray[cvNum]->destroyed = true;
							
								serverCVArray[cvNum]->lockNum = -1;				
							}
						}
					}
				}
			}
			else{
				activitySuccess = false;
				found = true;
			}
		}
		else if(atoi(arg[0]) == CONDITIONDESTROY){
			int lockNum;
			int cvNum;
			missing = CV;
			create = false;
			
			for(int a = 0;a < nextServerCV;a++){
				if(strcmp(serverCVArray[a]->name,arg[1]) == 0){
					printf("Names matched. Found CV\n");
					//replyNum = a;
					cvNum = a;
					replyNum = serverCVArray[a]->name;
					activitySuccess = true;
					found = true;
					break;
				}
			}
			
			if(found){
				if(cvNum < 0 || cvNum >= nextServerCV){
					printf("Invalid condition variable number\n");
					
					activitySuccess = false;
				}
				else if(serverCVArray[cvNum]->replyQ->IsEmpty()){
					printf("Condition %d destroyed\n",cvNum);
					serverCVArray[cvNum]->destroyed = true;
				}
				else{
					printf("Condition %d ready to destroy\n",cvNum);
					serverCVArray[cvNum]->readyToBeDestroyed = true;
				}
				
				//replyNum = 1;
						replyNum = "1";
				activitySuccess = true;
			}
		}
		else if(atoi(arg[0]) == MONITORCREATE){
			create = true;
			missing = MV;
			int arrayLength = atoi(arg[2]) ;
	
			for(int a = 0;a < nextServerMV;a++){
				if(strcmp(serverMVArray[a]->name,arg[1]) == 0){
					printf("Names matched. Found MV\n");
					//replyNum = a;
					replyNum = serverMVArray[a]->name;
					activitySuccess = true;
					found = true;
					break;
				}
			}
		}
		else if(atoi(arg[0]) == MONITORGET){
			int mvNum;
			create = false;
			missing = MV;
			
			for(int a = 0;a < nextServerMV;a++){
				if(strcmp(serverMVArray[a]->name,arg[1]) == 0){
					printf("Names matched. Found MV\n");
					//replyNum = a;
					mvNum = a;
					replyNum = serverMVArray[a]->name;
					activitySuccess = true;
					found = true;
					break;
				}
			}
		
			if(found){
				int index = atoi(arg[2]);
			
				if(mvNum < 0 || mvNum >=  nextServerMV){
					printf("Invalid monitor variable number\n");
					
					activitySuccess = false;
				}
				else if(serverMVArray[mvNum] == NULL){
					printf("This monitor variable does not exist anymore\n");
					
					activitySuccess = false;
				}
				else{
					printf("Getting monitor variable\n");
					activitySuccess= true;
					//replyNum = serverMVArray[mvNum]->values[index];
					replyNum = "1";
				}
			}
		}
		else if(atoi(arg[0]) == MONITORSET){
			int mvNum;
			create = false;
			missing = MV;
			
			for(int a = 0;a < nextServerMV;a++){
				if(strcmp(serverMVArray[a]->name,arg[1]) == 0){
					printf("Names matched. Found MV\n");
					//replyNum = a;
					mvNum = a;
					replyNum = serverMVArray[a]->name;
					activitySuccess = true;
					found = true;
					break;
				}
			}
		
			if(found){
				int index = atoi(arg[2]);
				int value = atoi(arg[3]);
			
				if(mvNum < 0 || mvNum >=  nextServerMV){
					printf("Invalid monitor variable number\n");
					
					activitySuccess = false;
				}
				else if(serverMVArray[mvNum] == NULL){
					printf("This monitor variable does not exist anymore\n");
					
					activitySuccess = false;
				}
				else{
					printf("Setting monitor variable\n");
					serverMVArray[mvNum]->values[index] = value;
					activitySuccess= true;
					//replyNum = 1;
						replyNum = "1";
				}
			}
		}
		else if(atoi(arg[0]) == MONITORDESTROY){
			int mvNum;
			create = false;
			missing = MV;
			
			for(int a = 0;a < nextServerMV;a++){
				if(strcmp(serverMVArray[a]->name,arg[1]) == 0){
					printf("Names matched. Found MV\n");
					//replyNum = a;
					mvNum = a;
					replyNum = serverMVArray[a]->name;
					activitySuccess = true;
					found = true;
					break;
				}
			}
			
			if(found){
				if(mvNum < 0 || mvNum >=  nextServerMV){
					printf("Invalid monitor variable number\n");
					
					activitySuccess = false;
				}
				else if(serverMVArray[mvNum] == NULL){
					printf("This monitor variable does not exist anymore\n");
					
					activitySuccess = false;
				}
				else{
					printf("Destroying monitor variable %d\n",mvNum);
					
					serverMVArray[mvNum] = NULL;
					
					activitySuccess= true;
					//replyNum = 1;
						replyNum = "1";
				}
			}
		}
		else{
			printf("Bad input to main server\n The msg is %s\n",saveInput);
			activitySuccess = false;
		}
				
		if(!found){
			if(missing == LOCK){
				sendMessage = false;
			
				serverQueryArrayLock->Acquire();
				
				serverQueries[nextServerQuery] = new ServerQuery();
				serverQueries[nextServerQuery]->id = nextServerQuery;
				serverQueries[nextServerQuery]->msg = saveInput;
				serverQueries[nextServerQuery]->resolved = false;
				serverQueries[nextServerQuery]->responses = new bool[numservers];
				serverQueries[nextServerQuery]->machineid = inPktHdr.from;
				serverQueries[nextServerQuery]->mailbox = inMailHdr.from;
				serverQueries[nextServerQuery]->createQuery = create;
				
				for(int a = 0;a < numservers;a++){
					if(a == servernumber)
						serverQueries[nextServerQuery]->responses[a] = true;
					else
						serverQueries[nextServerQuery]->responses[a] = false;
				}
				
				for(int a = 0;a < numservers;a++){
					if(serverQueries[nextServerQuery]->responses[a] == false){
						stringstream ss;
						ss << SERVERQUERY << " " << DIDYOUCREATELOCK << " " << arg[1] << " " << nextServerQuery << " " << serverQueries[nextServerQuery]->createQuery;
						string message = ss.str();
						
						char data[MaxMailSize];
						strcpy(data,message.c_str());
						outPktHdr.to = a;		
						outMailHdr.to = 1;
						outMailHdr.from = 0;
						outMailHdr.length = strlen(data) + 1;

						printf("Sending Server %d lock query\n",a);
						bool success = postOffice->Send(outPktHdr, outMailHdr, data); 

						if ( !success ) {
						  printf("The postOffice Send failed. You must not have the other Nachos running. Terminating Nachos.\n");
						  interrupt->Halt();
						}
					
					}
				}
				
				nextServerQuery++;
				
				serverQueryArrayLock->Release();
			
			}
			else if(missing = CV){
				sendMessage = false;
			
				serverQueryArrayLock->Acquire();
				
				serverQueries[nextServerQuery] = new ServerQuery();
				serverQueries[nextServerQuery]->id = nextServerQuery;
				serverQueries[nextServerQuery]->msg = saveInput;
				serverQueries[nextServerQuery]->resolved = false;
				serverQueries[nextServerQuery]->responses = new bool[numservers];
				serverQueries[nextServerQuery]->machineid = inPktHdr.from;
				serverQueries[nextServerQuery]->mailbox = inMailHdr.from;
				serverQueries[nextServerQuery]->createQuery = create;
				
				for(int a = 0;a < numservers;a++){
					if(a == servernumber)
						serverQueries[nextServerQuery]->responses[a] = true;
					else
						serverQueries[nextServerQuery]->responses[a] = false;
				}
				
				for(int a = 0;a < numservers;a++){
					if(serverQueries[nextServerQuery]->responses[a] == false){
						stringstream ss;
						ss << SERVERQUERY << " " << DIDYOUCREATECV << " " << arg[1] << " " << nextServerQuery << " " << serverQueries[nextServerQuery]->createQuery;
						string message = ss.str();
						
						char data[MaxMailSize];
						strcpy(data,message.c_str());
						outPktHdr.to = a;		
						outMailHdr.to = 1;
						outMailHdr.from = 0;
						outMailHdr.length = strlen(data) + 1;

						printf("Sending Server %d CV query\n",a);
						bool success = postOffice->Send(outPktHdr, outMailHdr, data); 

						if ( !success ) {
						  printf("The postOffice Send failed. You must not have the other Nachos running. Terminating Nachos.\n");
						  interrupt->Halt();
						}
					
					}
				}
				
				nextServerQuery++;
				
				serverQueryArrayLock->Release();
			}
			else if(missing = MV){
				sendMessage = false;
			
				serverQueryArrayLock->Acquire();
				
				serverQueries[nextServerQuery] = new ServerQuery();
				serverQueries[nextServerQuery]->id = nextServerQuery;
				serverQueries[nextServerQuery]->msg = saveInput;
				serverQueries[nextServerQuery]->resolved = false;
				serverQueries[nextServerQuery]->responses = new bool[numservers];
				serverQueries[nextServerQuery]->machineid = inPktHdr.from;
				serverQueries[nextServerQuery]->mailbox = inMailHdr.from;
				serverQueries[nextServerQuery]->createQuery = create;
				
				for(int a = 0;a < numservers;a++){
					if(a == servernumber)
						serverQueries[nextServerQuery]->responses[a] = true;
					else
						serverQueries[nextServerQuery]->responses[a] = false;
				}
				
				for(int a = 0;a < numservers;a++){
					if(serverQueries[nextServerQuery]->responses[a] == false){
						stringstream ss;
						ss << SERVERQUERY << " " << DIDYOUCREATEMV << " " << arg[1] << " " << nextServerQuery << " " << serverQueries[nextServerQuery]->createQuery;
						string message = ss.str();
						
						char data[MaxMailSize];
						strcpy(data,message.c_str());
						outPktHdr.to = a;		
						outMailHdr.to = 1;
						outMailHdr.from = 0;
						outMailHdr.length = strlen(data) + 1;

						printf("Sending Server %d MV query\n",a);
						bool success = postOffice->Send(outPktHdr, outMailHdr, data); 

						if ( !success ) {
						  printf("The postOffice Send failed. You must not have the other Nachos running. Terminating Nachos.\n");
						  interrupt->Halt();
						}
					
					}
				}
				
				nextServerQuery++;
				
				serverQueryArrayLock->Release();
			}
			else{
				printf("No setting of MISSING variable\n");
			}
		
		}	
	
		if(sendMessage){
			char data[MaxMailSize];
			if(activitySuccess)
				sprintf(data,"%s",replyNum);
			else
				sprintf(data,"%d",-1);
			
			//strcpy(data,replyNum);
			if(serverMsg){
				outPktHdr.to = machineTo;
				outMailHdr.to = mailboxTo;
			}
			else{
				outPktHdr.to = inPktHdr.from;		
				outMailHdr.to = inMailHdr.from;
			}
			outMailHdr.from = 0;
			outMailHdr.length = strlen(data) + 1;
			bool success = postOffice->Send(outPktHdr, outMailHdr, data); 

			if ( !success ) {
			  printf("The postOffice Send failed. You must not have the other Nachos running. Terminating Nachos.\n");
			  interrupt->Halt();
			}
		}
	}
}

void SecondServer(int numservers){
	PacketHeader outPktHdr, inPktHdr;
    MailHeader outMailHdr, inMailHdr;
	char input[MaxMailSize];
	
	printf("Second server has started\n");
	
	while(true){
		char* arg[5];
		
		postOffice->Receive(1, &inPktHdr, &inMailHdr,input);
		
		char* buffer  = strtok(input," ");
		
		int i = 0;
		while(buffer != NULL){
			arg[i] = buffer;
			i++;
			buffer = strtok(NULL," ");
		}		
		
		if(atoi(arg[0]) == SERVERQUERY){
			int query = atoi(arg[1]);
			
			if(query == DIDYOUCREATELOCK){
				printf("Server %d asks if I created lock %s id = %d\n",inPktHdr.from,arg[2],atoi(arg[3]));
			
				bool mylock = false;
				
				int locknum;
				serverLockArrayLock->Acquire();
				for(int a = 0;a < nextServerLock;a++){
					if(strcmp(serverLockArray[a]->name,arg[2]) == 0){
						locknum = a;
						mylock = true;
						printf("Server %d Found the lock\n",servernumber);
						break;
					}
				}			
				serverLockArrayLock->Release();
			
				if(mylock){
					char data[MaxMailSize];
					stringstream ss;
					ss << SERVERRESPONSE << " " << MINE << " " << arg[3];
					string messageTemp = ss.str();
					strcpy(data,messageTemp.c_str());
					
					outPktHdr.to = inPktHdr.from;		
					outMailHdr.to = 1;
					outMailHdr.from = 1;
					outMailHdr.length = strlen(data) + 1;

					printf("Sending confirmation of ownership to server %d\n",inPktHdr.from);
					bool success = postOffice->Send(outPktHdr, outMailHdr, data); 

					if ( !success ) {
					  printf("The postOffice Send failed. You must not have the other Nachos running. Terminating Nachos.\n");
					  interrupt->Halt();
					}				

					
				}
				else{
					printf("I(Server %d) did not make the lock\n",servernumber);
				
					char data[MaxMailSize];
					
					stringstream ss;
					ss << SERVERRESPONSE << " " << NOTMYLOCK << " " << arg[3];
					string messageTemp = ss.str();
					strcpy(data,messageTemp.c_str());
					
					outPktHdr.to = inPktHdr.from;		
					outMailHdr.to = 1;
					outMailHdr.from = 1;
					outMailHdr.length = strlen(data) + 1;

					bool success = postOffice->Send(outPktHdr, outMailHdr, data); 

					if ( !success ) {
					  printf("The postOffice Send failed. You must not have the other Nachos running. Terminating Nachos.\n");
					  interrupt->Halt();
					}
				}
			}
			else if(query == DIDYOUCREATECV){
				printf("Server %d asks if I created CV %s id = %d\n",inPktHdr.from,arg[2],atoi(arg[3]));
			
				bool myCV = false;
				
				int CVnum;
				serverCVArrayLock->Acquire();
				for(int a = 0;a < nextServerCV;a++){
					if(strcmp(serverCVArray[a]->name,arg[2]) == 0){
						CVnum = a;
						myCV = true;
						printf("Server %d Found the CV\n",servernumber);
						break;
					}
				}			
				serverCVArrayLock->Release();
			
				if(myCV){
					char data[MaxMailSize];
					stringstream ss;
					ss << SERVERRESPONSE << " " << MINE << " " << arg[3];
					string messageTemp = ss.str();
					strcpy(data,messageTemp.c_str());
					
					outPktHdr.to = inPktHdr.from;		
					outMailHdr.to = 1;
					outMailHdr.from = 1;
					outMailHdr.length = strlen(data) + 1;

					printf("Sending confirmation of ownership to server %d, the boolean is %d\n",outPktHdr.to,arg[4]);
					bool success = postOffice->Send(outPktHdr, outMailHdr, data); 

					if ( !success ) {
					  printf("The postOffice Send failed. You must not have the other Nachos running. Terminating Nachos.\n");
					  interrupt->Halt();
					}				

					
				}
				else{
					printf("I(Server %d) did not make the CV\n",servernumber);
				
					char data[MaxMailSize];
					
					stringstream ss;
					ss << SERVERRESPONSE << " " << NOTMYCV << " " << arg[3];
					string messageTemp = ss.str();
					strcpy(data,messageTemp.c_str());
					
					outPktHdr.to = inPktHdr.from;		
					outMailHdr.to = 1;
					outMailHdr.from = 1;
					outMailHdr.length = strlen(data) + 1;

					bool success = postOffice->Send(outPktHdr, outMailHdr, data); 

					if ( !success ) {
					  printf("The postOffice Send failed. You must not have the other Nachos running. Terminating Nachos.\n");
					  interrupt->Halt();
					}
				}
			
			}
		}
		else if(atoi(arg[0]) == SERVERRESPONSE){
			int response = atoi(arg[1]);
			int queryid = atoi(arg[2]);
			
			if(response == NOTMYLOCK){
				serverQueryArrayLock->Acquire();
				
				printf("Recieved response from %d. Not the owner of lock %s\n",inPktHdr.from,arg[2]);
				
				serverQueries[queryid]->responses[inPktHdr.from] = true;
				
				bool allreplies = true;				
				for(int a = 0;a < numservers;a++){
					if(serverQueries[queryid]->responses[a] == false)
						allreplies = false;				
				}
				
				if(allreplies && !serverQueries[queryid]->resolved){
					serverLockArrayLock->Acquire();
					
					serverQueries[queryid]->resolved = true;
					
					char data[MaxMailSize];
					if(serverQueries[queryid]->createQuery){
						char tempinput[MaxMailSize];
					
						strcpy(tempinput,serverQueries[queryid]->msg);
						char* tempbuffer = strtok(tempinput," ");
						char* temparg[3];
			
						int a = 0;
						while(tempbuffer != NULL){
							temparg[a] = tempbuffer;
							a++;
							tempbuffer = strtok(NULL," ");
						}	
										
						printf("Creating lock\n");
						
						serverLockArray[nextServerLock] = new ServerLock();
						
						serverLockArray[nextServerLock]->available = true;
						serverLockArray[nextServerLock]->destroyed = false;
						serverLockArray[nextServerLock]->owner = -1;
						serverLockArray[nextServerLock]->replyQ = new List;
											
						int length = strlen(temparg[1]);
																					
						char* tempchar = new char[length];
						strcpy(tempchar,temparg[1]);
						
						serverLockArray[nextServerLock]->name = tempchar;
						
						//replyNum = tempchar;
						//replyNum = nextServerLock;
						//activitySuccess = true;
						
						nextServerLock++;
						serverLockArrayLock->Release();
				
						strcpy(data,tempchar);
					}
					else{
						printf("Lock does not exist\n");
						strcpy(data,"-1");
					}
					
					outPktHdr.to = serverQueries[queryid]->machineid;	
					outMailHdr.to = serverQueries[queryid]->mailbox;
					outMailHdr.from = 1;
					outMailHdr.length = strlen(data) + 1;

					bool success = postOffice->Send(outPktHdr, outMailHdr, data); 

					if ( !success ) {
					  printf("The postOffice Send failed. You must not have the other Nachos running. Terminating Nachos.\n");
					  interrupt->Halt();
					}
				
				}
				
				serverQueryArrayLock->Release();			
			}
			else if(response == NOTMYCV){
				serverQueryArrayLock->Acquire();
				
				printf("Recieved response from %d. Not the owner of CV %s\n",inPktHdr.from,arg[2]);
				
				serverQueries[queryid]->responses[inPktHdr.from] = true;
				
				bool allreplies = true;				
				for(int a = 0;a < numservers;a++){
					if(serverQueries[queryid]->responses[a] == false)
						allreplies = false;				
				}
				
				if(allreplies && !serverQueries[queryid]->resolved){
					serverCVArrayLock->Acquire();
					
					serverQueries[queryid]->resolved = true;
					
					char data[MaxMailSize];
					if(serverQueries[queryid]->createQuery){
						char tempinput[MaxMailSize];
					
						strcpy(tempinput,serverQueries[queryid]->msg);
						char* tempbuffer = strtok(tempinput," ");
						char* temparg[3];
			
						int a = 0;
						while(tempbuffer != NULL){
							temparg[a] = tempbuffer;
							a++;
							tempbuffer = strtok(NULL," ");
						}	
										
						printf("Creating CV\n");
						
						serverCVArray[nextServerCV] = new ServerCV();
						
						serverCVArray[nextServerCV]->replyQ = new List();
						serverCVArray[nextServerCV]->destroyed = false;
						serverCVArray[nextServerCV]->lockNum = -1;
						serverCVArray[nextServerCV]->readyToBeDestroyed = false;
											
						int length = strlen(temparg[1]);
																					
						char* tempchar = new char[length];
						strcpy(tempchar,temparg[1]);
						
						serverCVArray[nextServerCV]->name = tempchar;
						
						//replyNum = tempchar;
						//replyNum = nextServerLock;
						//activitySuccess = true;
						
						nextServerCV++;
						serverCVArrayLock->Release();
				
						strcpy(data,tempchar);
					}
					else{
						printf("CV does not exist\n");
						strcpy(data,"-1");
					}
					
					outPktHdr.to = serverQueries[queryid]->machineid;	
					outMailHdr.to = serverQueries[queryid]->mailbox;
					outMailHdr.from = 1;
					outMailHdr.length = strlen(data) + 1;

					bool success = postOffice->Send(outPktHdr, outMailHdr, data); 

					if ( !success ) {
					  printf("The postOffice Send failed. You must not have the other Nachos running. Terminating Nachos.\n");
					  interrupt->Halt();
					}
				
				}
				
				serverQueryArrayLock->Release();
			}
			else if(response == NOTMYMV){
				serverQueryArrayLock->Acquire();
				
				printf("Recieved response from %d. Not the owner of MV %s\n",inPktHdr.from,arg[2]);
				
				serverQueries[queryid]->responses[inPktHdr.from] = true;
				
				bool allreplies = true;				
				for(int a = 0;a < numservers;a++){
					if(serverQueries[queryid]->responses[a] == false)
						allreplies = false;				
				}
				
				if(allreplies && !serverQueries[queryid]->resolved){
					serverMVArrayLock->Acquire();
					
					serverQueries[queryid]->resolved = true;
					
					char data[MaxMailSize];
					if(serverQueries[queryid]->createQuery){
						char tempinput[MaxMailSize];
					
						strcpy(tempinput,serverQueries[queryid]->msg);
						char* tempbuffer = strtok(tempinput," ");
						char* temparg[3];
			
						int a = 0;
						while(tempbuffer != NULL){
							temparg[a] = tempbuffer;
							a++;
							tempbuffer = strtok(NULL," ");
						}	
										
						printf("Creating MV\n");
						
						int arrayLength = atoi(arg[2]) ;
						
						serverMVArray[nextServerMV] = new ServerMV();
					
						serverMVArray[nextServerMV]->values = new int[arrayLength];
						for(int b = 0;b < arrayLength;b++){
							serverMVArray[nextServerMV]->values[b] = 0;
						}
											
						int length = strlen(temparg[1]);
																					
						char* tempchar = new char[length];
						strcpy(tempchar,temparg[1]);
						
						serverMVArray[nextServerMV]->name = tempchar;
						
						//replyNum = tempchar;
						//replyNum = nextServerLock;
						//activitySuccess = true;
						
						nextServerMV++;
						serverMVArrayLock->Release();
				
						strcpy(data,tempchar);
					}
					else{
						printf("MV does not exist\n");
						strcpy(data,"-1");
					}
					
					outPktHdr.to = serverQueries[queryid]->machineid;	
					outMailHdr.to = serverQueries[queryid]->mailbox;
					outMailHdr.from = 1;
					outMailHdr.length = strlen(data) + 1;

					bool success = postOffice->Send(outPktHdr, outMailHdr, data); 

					if ( !success ) {
					  printf("The postOffice Send failed. You must not have the other Nachos running. Terminating Nachos.\n");
					  interrupt->Halt();
					}
				
				}
				
				serverQueryArrayLock->Release();
			}
			else if(response == MINE){
				serverQueryArrayLock->Acquire();
				
				printf("Changing query %d status to resolved\n",queryid);
				serverQueries[queryid]->resolved = true;	
				
				char data[MaxMailSize];
				stringstream ss;
				ss << SERVERMSG << " " << serverQueries[queryid]->machineid << " " << serverQueries[queryid]->mailbox << " " << serverQueries[queryid]->msg;
				string messageTemp = ss.str();
				strcpy(data,messageTemp.c_str());
				
				outPktHdr.to = inPktHdr.from;		
				outMailHdr.to = 0;
				outMailHdr.from = 1;
				outMailHdr.length = strlen(data) + 1;

				bool success = postOffice->Send(outPktHdr, outMailHdr, data); 

				if ( !success ) {
				  printf("The postOffice Send failed. You must not have the other Nachos running. Terminating Nachos.\n");
				  interrupt->Halt();
				}

				serverQueryArrayLock->Release();
			}
		
		}
		else{
			printf("Bad input\n");
		}
	}
}

bool mine(char* object){
	return false;
}

void TestClient(int numservers){
	
	int request;
	printf("Input number:");
	cin >> request;
	
	
		PacketHeader outPktHdr, inPktHdr;
		MailHeader outMailHdr, inMailHdr;
		char *data = "3";
		char buffer[MaxMailSize];	
		
	
	if(request == 3){
		bool success;
		outPktHdr.to = 0;		
		outMailHdr.to = 0;
		outMailHdr.from = 0;
		outMailHdr.length = strlen("3 Bob") + 1;
		// Send the first message
		success = postOffice->Send(outPktHdr, outMailHdr, "3 Bob"); 
		if ( !success ) {
		  printf("The postOffice Send failed. You must not have the other Nachos running. Terminating Nachos.\n");
		  interrupt->Halt();
		}
	
		postOffice->Receive(0, &inPktHdr, &inMailHdr, buffer);
	
		printf("My lock name is %s\n",buffer);
		
		outPktHdr.to = 1;		
		outMailHdr.to = 0;
		outMailHdr.from = 0;
		outMailHdr.length = strlen("5 BobCV") + 1;
		// Send the first message
		success = postOffice->Send(outPktHdr, outMailHdr, "5 BobCV"); 
		if ( !success ) {
		  printf("The postOffice Send failed. You must not have the other Nachos running. Terminating Nachos.\n");
		  interrupt->Halt();
		}
	
		postOffice->Receive(0, &inPktHdr, &inMailHdr, buffer);
	
		printf("My CV name is %s\n",buffer);
		
		outPktHdr.to = 1;		
		outMailHdr.to = 0;
		outMailHdr.from = 0;
		outMailHdr.length = strlen("6 BobCV Bob") + 1;
		// Send the first message
		success = postOffice->Send(outPktHdr, outMailHdr, "6 BobCV Bob"); 
		if ( !success ) {
		  printf("The postOffice Send failed. You must not have the other Nachos running. Terminating Nachos.\n");
		  interrupt->Halt();
		}
	
		postOffice->Receive(0, &inPktHdr, &inMailHdr, buffer);
	
		printf("My second destroy lock name is %s\n",buffer);

		
		
	}
	else if(request == 0){
		
		
		bool success;
		outPktHdr.to = 0;		
		outMailHdr.to = 1;
		outMailHdr.from = 1;
		outMailHdr.length = strlen("0 0") + 1;
		// Send the first message
		success = postOffice->Send(outPktHdr, outMailHdr, "0 0"); 
		if ( !success ) {
		  printf("The postOffice Send failed. You must not have the other Nachos running. Terminating Nachos.\n");
		  interrupt->Halt();
		}
	
		postOffice->Receive(0, &inPktHdr, &inMailHdr, buffer);
		
		printf("Fine\n");
	}
	else if(request == 1){
		printf("Signalling condition\n");
		
		outPktHdr.to = 0;		
		outMailHdr.to = 0;
		outMailHdr.from = 1;
		outMailHdr.length = strlen("0 0") + 1;
		// Send the first message
		bool success = postOffice->Send(outPktHdr, outMailHdr, "0 0"); 
		if ( !success ) {
		  printf("The postOffice Send failed. You must not have the other Nachos running. Terminating Nachos.\n");
		  interrupt->Halt();
		}
		
		postOffice->Receive(0, &inPktHdr, &inMailHdr, buffer);
		
		outPktHdr.to = 0;		
		outMailHdr.to = 0;
		outMailHdr.from = 1;
		outMailHdr.length = strlen("6 0 0") + 1;
		// Send the first message
		success = postOffice->Send(outPktHdr, outMailHdr, "6 0 0"); 
		if ( !success ) {
		  printf("The postOffice Send failed. You must not have the other Nachos running. Terminating Nachos.\n");
		  interrupt->Halt();
		}

		postOffice->Receive(0, &inPktHdr, &inMailHdr, buffer);
		
		outPktHdr.to = 0;		
		outMailHdr.to = 0;
		outMailHdr.from = 1;
		outMailHdr.length = strlen("1 0") + 1;
		// Send the first message
		success = postOffice->Send(outPktHdr, outMailHdr, "1 0"); 
		if ( !success ) {
		  printf("The postOffice Send failed. You must not have the other Nachos running. Terminating Nachos.\n");
		  interrupt->Halt();
		}

		postOffice->Receive(0, &inPktHdr, &inMailHdr, buffer);
		
		Delay(5);
		
		printf("Broadcasting condition");
		outPktHdr.to = 0;		
		outMailHdr.to = 0;
		outMailHdr.from = 1;
		outMailHdr.length = strlen("0 0") + 1;
		// Send the first message
		success = postOffice->Send(outPktHdr, outMailHdr, "0 0"); 
		if ( !success ) {
		  printf("The postOffice Send failed. You must not have the other Nachos running. Terminating Nachos.\n");
		  interrupt->Halt();
		}
		
		postOffice->Receive(0, &inPktHdr, &inMailHdr, buffer);
		
		outPktHdr.to = 0;		
		outMailHdr.to = 0;
		outMailHdr.from = 1;
		outMailHdr.length = strlen("7 0 0") + 1;
		// Send the first message
		success = postOffice->Send(outPktHdr, outMailHdr, "7 0 0"); 
		if ( !success ) {
		  printf("The postOffice Send failed. You must not have the other Nachos running. Terminating Nachos.\n");
		  interrupt->Halt();
		}
		
		postOffice->Receive(0, &inPktHdr, &inMailHdr, buffer);
		
		outPktHdr.to = 0;		
		outMailHdr.to = 0;
		outMailHdr.from = 1;
		outMailHdr.length = strlen("1 0") + 1;
		// Send the first message
		success = postOffice->Send(outPktHdr, outMailHdr, "1 0"); 
		if ( !success ) {
		  printf("The postOffice Send failed. You must not have the other Nachos running. Terminating Nachos.\n");
		  interrupt->Halt();
		}
		
		postOffice->Receive(0, &inPktHdr, &inMailHdr, buffer);
		
	}
	else if(request == 2){
		outPktHdr.to = 0;		
		outMailHdr.to = 0;
		outMailHdr.from = 1;
		outMailHdr.length = strlen("9 50") + 1;
		// Send the first message
		bool success = postOffice->Send(outPktHdr, outMailHdr, "9 50"); 
		if ( !success ) {
		  printf("The postOffice Send failed. You must not have the other Nachos running. Terminating Nachos.\n");
		  interrupt->Halt();
		}
		
		postOffice->Receive(0, &inPktHdr, &inMailHdr, buffer);	
		
		outPktHdr.to = 0;		
		outMailHdr.to = 0;
		outMailHdr.from = 1;
		outMailHdr.length = strlen("12 0") + 1;
		// Send the first message
		success = postOffice->Send(outPktHdr, outMailHdr, "12 0"); 
		if ( !success ) {
		  printf("The postOffice Send failed. You must not have the other Nachos running. Terminating Nachos.\n");
		  interrupt->Halt();
		}
		
		postOffice->Receive(0, &inPktHdr, &inMailHdr, buffer);	
		
		outPktHdr.to = 0;		
		outMailHdr.to = 0;
		outMailHdr.from = 1;
		outMailHdr.length = strlen("11 0 10 555") + 1;
		// Send the first message
		success = postOffice->Send(outPktHdr, outMailHdr, "11 0 10 555"); 
		if ( !success ) {
		  printf("The postOffice Send failed. You must not have the other Nachos running. Terminating Nachos.\n");
		  interrupt->Halt();
		}
		
		postOffice->Receive(0, &inPktHdr, &inMailHdr, buffer);	
		
		outPktHdr.to = 0;		
		outMailHdr.to = 0;
		outMailHdr.from = 1;
		outMailHdr.length = strlen("10 0 10") + 1;
		// Send the first message
		success = postOffice->Send(outPktHdr, outMailHdr, "10 0 10"); 
		if ( !success ) {
		  printf("The postOffice Send failed. You must not have the other Nachos running. Terminating Nachos.\n");
		  interrupt->Halt();
		}
		
		postOffice->Receive(0, &inPktHdr, &inMailHdr, buffer);	
		
		printf("%s\n",buffer);
	}
}

void
MailTest(int farAddr)
{

	PacketHeader outPktHdr, inPktHdr;
    MailHeader outMailHdr, inMailHdr;
	char *data = "3";
    char buffer[MaxMailSize];	
	
	outPktHdr.to = 0;		
    outMailHdr.to = 0;
    outMailHdr.from = 1;
    outMailHdr.length = strlen("3") + 1;
	// Send the first message
    bool success = postOffice->Send(outPktHdr, outMailHdr, "3"); 
	if ( !success ) {
      printf("The postOffice Send failed. You must not have the other Nachos running. Terminating Nachos.\n");
      interrupt->Halt();
    }
	
	postOffice->Receive(0, &inPktHdr, &inMailHdr, buffer);
	 
	
	//char* lock = atoi(buffer);
	
	data = strcat("0",buffer);
	outPktHdr.to = 0;		
    outMailHdr.to = 0;
    outMailHdr.from = 1;
    outMailHdr.length = strlen(data) + 1;
	// Send the first message
    success = postOffice->Send(outPktHdr, outMailHdr, "00"); 
	if ( !success ) {
      printf("The postOffice Send failed. You must not have the other Nachos running. Terminating Nachos.\n");
      interrupt->Halt();
    }

	postOffice->Receive(0, &inPktHdr, &inMailHdr, buffer);
	printf("Acquired lock\n");
	
    outPktHdr.to = 0;		
    outMailHdr.to = 0;
    outMailHdr.from = 1;
    outMailHdr.length = strlen("5") + 1;
	// Send the first message
    success = postOffice->Send(outPktHdr, outMailHdr, "5"); 
	if ( !success ) {
      printf("The postOffice Send failed. You must not have the other Nachos running. Terminating Nachos.\n");
      interrupt->Halt();
    }
	
	postOffice->Receive(0, &inPktHdr, &inMailHdr, buffer);
	printf("Recieved the condition number\n");
	
	outPktHdr.to = 0;		
    outMailHdr.to = 0;
    outMailHdr.from = 1;
    outMailHdr.length = strlen("200") + 1;
	// Send the first message
    success = postOffice->Send(outPktHdr, outMailHdr, "200"); 
	if ( !success ) {
      printf("The postOffice Send failed. You must not have the other Nachos running. Terminating Nachos.\n");
      interrupt->Halt();
    }	
	
	printf("How do I get past this\n");
	
	postOffice->Receive(0, &inPktHdr, &inMailHdr, buffer);
	
	printf("I'm awake\n");
	
	outPktHdr.to = 0;		
    outMailHdr.to = 0;
    outMailHdr.from = 1;
    outMailHdr.length = strlen("10") + 1;
	// Send the first message
    success = postOffice->Send(outPktHdr, outMailHdr, "10"); 
	if ( !success ) {
      printf("The postOffice Send failed. You must not have the other Nachos running. Terminating Nachos.\n");
      interrupt->Halt();
    }
}
